package work.xiaohong.search.controller;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import work.xiaohong.search.config.ElasticsearchConfig;
import work.xiaohong.search.entity.Result;
import work.xiaohong.search.entity.StatusCode;

import java.util.Map;


@RequestMapping("/index")
@RestController
public class IndexController {

    @Autowired
    private RestHighLevelClient restHighLevelClient;


    @GetMapping("/exists/{index}")
    public Result<Boolean> exists(@PathVariable(name = "index") String index) throws Exception {
        GetIndexRequest request = new GetIndexRequest(index);
        request.local(false); //是否返回本地信息或从主节点检索状态
        request.humanReadable(true); //以适合人类的格式返回结果
        request.includeDefaults(false); //是否为每个索引返回所有默认设置
        boolean exists = restHighLevelClient.indices().exists(request, ElasticsearchConfig.COMMON_OPTIONS);
        // 关闭客户端
//        restHighLevelClient.close();
        return new Result<Boolean>(true, StatusCode.OK,"查询成功",exists);
    }

    @DeleteMapping("/{index}")
    public Result delete(@PathVariable(name = "index") String index) throws Exception {
        DeleteIndexRequest request = new DeleteIndexRequest(index);
        if (exists(index).getData()){
            AcknowledgedResponse response = restHighLevelClient.indices().delete(request, ElasticsearchConfig.COMMON_OPTIONS);
            if (response.isAcknowledged()){
                // 关闭客户端
//                restHighLevelClient.close();
                return new Result(true, StatusCode.OK,"删除成功");
            }else {
                return new Result(true, StatusCode.ERROR,"删除失败");
            }
        }else {
            return new Result(false, StatusCode.ERROR,"索引不存在");
        }
    }

    @Data
    @AllArgsConstructor
    class Message{
        private String message;
    }

    /**
     * 创建索引
     * @param index
     * @return
     * @throws Exception
     */
    @PostMapping("/{index}")
    public Result creat(@PathVariable(name = "index") String index) throws Exception {
        CreateIndexRequest request = new CreateIndexRequest(index);
//        request.settings(Settings.builder()
//                .put("index.number_of_shards", 3)
//                .put("index.number_of_replicas", 2)
//        );
        if (exists(index).getData()){
            // 更新
            return new Result(false, StatusCode.ERROR,"索引已存在");
        }else {
            // 新建
            CreateIndexResponse response = restHighLevelClient.indices().create(request, ElasticsearchConfig.COMMON_OPTIONS);
            if (response.isAcknowledged()){
                return new Result(false, StatusCode.OK,"索引创建成功");
            }else {
                return new Result(false, StatusCode.ERROR,"索引创建失败");
            }
        }
    }

    /**
     * 带mapping创建索引
     * @param index
     * @param mapping
     * @return
     * @throws Exception
     */
    @PostMapping("/mapping/{index}")
    private Result creatWithMapping(@PathVariable(name = "index") String index, @RequestBody Map<String,Map<String, Map<String,Object>>> mapping) throws Exception {
        CreateIndexRequest request = new CreateIndexRequest(index);

        if (exists(index).getData()){
            // 新建
            return new Result(false, StatusCode.ERROR,"索引已存在");
        }else {
            // 创建mapping
            request.mapping(mapping);
            CreateIndexResponse response = restHighLevelClient.indices().create(request, ElasticsearchConfig.COMMON_OPTIONS);
            if (response.isAcknowledged()){
                return new Result(false, StatusCode.OK,"创建索引及Mapping成功");
            }else {
                return new Result(false, StatusCode.ERROR,"创建索引及Mapping失败");
            }

        }
    }



}
